package timing.ukulele.third.service.common;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.service.WxOAuth2Service;
import org.springframework.stereotype.Service;
import timing.ukulele.common.dictionary.ThirdPartyTypeEnum;
import timing.ukulele.third.config.mp.WxMpConfiguration;

@Service
public class ThirdCommonService {

    /**
     * 根据thirdType/appid/code获取第三方平台用户唯一标识
     * @param appId
     * @param code
     * @return
     * @throws WxErrorException
     */
    public String getPlatIdByCode(Integer thirdType, String appId, String code) throws WxErrorException {
        String openId = null;
        if(ThirdPartyTypeEnum.WX_MP.getValue().equals(thirdType)) {
            WxOAuth2Service oAuth2Service = WxMpConfiguration.getMpServiceByAppId(appId).getOAuth2Service();
            openId = oAuth2Service.getAccessToken(code).getOpenId();
        }else if(ThirdPartyTypeEnum.WX_WEB.getValue().equals(thirdType)) {

        } else if(ThirdPartyTypeEnum.WX_APP.getValue().equals(thirdType)) {

        }
        return openId;
    }

}
